<?php
General::getTpl()->assign('extraTitle',"Navigations-Links");

if(!isset($_GET['catId']) || !is_numeric($_GET['catId'])) exit;

// Get CatName
$query = DB::getInstance()->query("
	SELECT 
		".Tables::$naviCatTbl.".catName,
		".Tables::$naviTbl.".naviName,
		".Tables::$naviTbl.".id
	FROM 
		".Tables::$naviCatTbl."
		LEFT JOIN
			".Tables::$naviTbl."
		ON
			".Tables::$naviCatTbl.".naviId = ".Tables::$naviTbl.".id
	WHERE 
		".Tables::$naviCatTbl.".id = '".$_GET['catId']."'");
$catName = DB::getInstance()->fetchArray($query);

General::getTpl()->assign('catId',$_GET['catId']);
General::getTpl()->assign('catName',$catName['catName']);
General::getTpl()->assign('navId',$catName['id']);
General::getTpl()->assign('navName',$catName['naviName']);

if(!Admin::checkPermission('canEditNavigation')) {
	General::ouputMessage('Fehler','Sie haben nicht gen&uuml;gend Rechte um diese Seite zu betrachten');
}else{
	
	
	/**
	 * NEW LINK
	 */
	if(isset($_GET['do']) && $_GET['do'] == "new") {
		require_once 'kernel/form.php';
		
		// Form-Data
		$i = 0;
		$formField = array();
		$formField[$i]['fieldName'] = "Name";
		$formField[$i]['fieldType'] = Form::$TYPE_TEXT;
		$formField[$i]['fieldDefaultValue'] = "";
		$formField[$i]['fieldCheck'] = true;
		$formField[$i]['fieldCheckExtra'] = Form::$CHECK_TEXT_NOTEMPTY;
		$i++;
		
		
		$predefLinkText = "Kein vorgefertiger Link::--::";
		$predefLinks = Navigation::getPredefinedLinks();
		foreach($predefLinks as $preLink) {
			$predefLinkText .= $preLink['preName']."::--::";
		}
		// Delete last comma
		$predefLinkText = substr($predefLinkText,0,strlen($predefLinkText)-6);
		
		$formField[$i]['fieldName'] = "Vorgefertigter Link";
		$formField[$i]['formName'] = "preDefined";
		$formField[$i]['fieldType'] = Form::$TYPE_SELECT;
		$formField[$i]['fieldDefaultValue'] = "";
		$formField[$i]['fieldCheck'] = false;
		$formField[$i]['fieldSelectOptions'] = $predefLinkText;
		$formField[$i]['fieldSelectMultiple'] = false;
		$i++;
		
		$formField[$i]['fieldName'] = "URL";
		$formField[$i]['fieldType'] = Form::$TYPE_TEXT;
		$formField[$i]['fieldDefaultValue'] = "";
		$formField[$i]['fieldCheck'] = false;
		$i++;
		
		$formField[$i]['fieldName'] = "Template";
		$formField[$i]['formName'] = "template";
		$formField[$i]['fieldType'] = Form::$TYPE_SELECT;
		$formField[$i]['fieldDefaultValue'] = "";
		$formField[$i]['fieldCheck'] = false;
		
		$templateKeys = "";
		$templateValues = "";
		foreach(Navigation::$naviTemplates as $key=>$template) {
			$templateValues .= $key."::--::";
			$templateKeys .= $template."::--::";
		}
		$templateKeys = substr($templateKeys,0,strlen($templateKeys)-6);
		$templateValues = substr($templateValues,0,strlen($templateValues)-6);
		$formField[$i]['fieldSelectOptions'] = "Kein Template::--::".$templateValues;
		$formField[$i]['fieldSelectKey'] = "::--::".$templateKeys;
		
		$formField[$i]['fieldSelectMultiple'] = false;
		$i++;
	
	
		$formField[$i]['fieldName'] = "HTML-Code";
		$formField[$i]['formName'] = "htmlCode";
		$formField[$i]['fieldDescription'] = "Wenn du hier HTML-Code angibst, wird der statt dem normalen Link ausgegeben.";
		$formField[$i]['fieldSelectOptions'] = "30_5";
		$formField[$i]['fieldType'] = Form::$TYPE_TEXTAREA;
		$formField[$i]['fieldDefaultValue'] = "";
		$formField[$i]['fieldCheck'] = false;
		$i++;
	
		
		$formField[$i]['fieldName'] = "Aktiv?";
		$formField[$i]['formName'] = "aktiv";
		$formField[$i]['fieldType'] = Form::$TYPE_SELECT;
		$formField[$i]['fieldDefaultValue'] = "Ja";
		$formField[$i]['fieldCheck'] = false;
		$formField[$i]['fieldSelectOptions'] = "Ja::--::Nein";
		$formField[$i]['fieldSelectMultiple'] = false;
		$i++;
	
		
		$formField[$i]['fieldName'] = "Tab?";
		$formField[$i]['formName'] = "isTab";
		$formField[$i]['fieldDescription'] = "Wenn der Link ein Tab ist, kann eingestellt werden, dass eine Kategorie nur angezeigt wird, wenn der Link angeklickt wurde.";
		$formField[$i]['fieldType'] = Form::$TYPE_SELECT;
		$formField[$i]['fieldDefaultValue'] = "Nein";
		$formField[$i]['fieldCheck'] = false;
		$formField[$i]['fieldSelectOptions'] = "Ja::--::Nein";
		$formField[$i]['fieldSelectMultiple'] = false;
		$i++;
		
		$formField[$i]['fieldName'] = "Nur f&uuml;r Admins?";
		$formField[$i]['formName'] = "onlyAdmins";
		$formField[$i]['fieldType'] = Form::$TYPE_SELECT;
		$formField[$i]['fieldDefaultValue'] = "Nein";
		$formField[$i]['fieldCheck'] = false;
		$formField[$i]['fieldSelectOptions'] = "Ja::--::Nein";
		$formField[$i]['fieldSelectMultiple'] = false;
		
		$form = new Form();
		$form->initByData("Link erstellen",true,"Speichern!",$formField);
		
		// Done?
		if($form->completeCheck()) {
			
			// Create Navigation
			$active = ($_POST['aktiv'][0] == "Ja") ? 0:1;
			// Is Tab?
			$isTab = ($_POST['isTab'][0] == "Ja") ? 0:1;
			// Only Admins can see this link?
			$onlyAdmins = ($_POST['onlyAdmins'][0] == "Ja") ? 0:1;
			// Template?
			$template = ($_POST['template'][0] == "Kein Template") ? "":$_POST['template'][0];
			
			$url = $_POST['URL'];
			if($_POST['preDefined'][0] != "Kein vorgefertiger Link") {
				// PredefinedLink selected
				foreach($predefLinks as $preL) {
					if($preL['preName'] == $_POST['preDefined'][0]) {
						if(Settings::getInstance()->getSetting("general.useModRewrite") == 0) {
							$url = $preL['preLinkModRewrite'];
						}else{
							$url = $preL['preLinkNoModRewrite'];
						}
					}
				}
			}
			
			Navigation::createLink($_GET['catId'],$_POST['Name'],$url,$template,$_POST['htmlCode'],$active,$isTab,$onlyAdmins);
			General::ouputMessage('Erfolgreich','Der Link wurde erfolgreich gespeichert.','form');
			
		}else{
			$form->outputFormular('form','formular.html');
		}
	}
	

	/**
	 * EDIT LINK
	 */
	if(isset($_GET['do']) && $_GET['do'] == "edit" && isset($_GET['linkId']) && is_numeric($_GET['linkId'])) {
		require_once 'kernel/form.php';
		
		// Get Data
		$query = DB::getInstance()->query("
			SELECT
				linkCatId,
				linkName,
				linkURL,
				linkTemplate,
				linkHTML,
				linkShow,
				linkTab,
				linkOnlyAdmin
			FROM
				".Tables::$naviLinksTbl."
			WHERE
				id = '".DB::parseVar($_GET['linkId'])."'");
		$data = DB::getInstance()->fetchArray($query);
		
		// Form-Data
		$i = 0;
		$formField = array();
		$formField[$i]['fieldName'] = "Name";
		$formField[$i]['fieldType'] = Form::$TYPE_TEXT;
		$formField[$i]['fieldDefaultValue'] = $data['linkName'];
		$formField[$i]['fieldCheck'] = true;
		$formField[$i]['fieldCheckExtra'] = Form::$CHECK_TEXT_NOTEMPTY;
		$i++;
		
		// Category
		$catData = "";
		$selCat = "";
		$cats = Navigation::getAllNavigationCats();
		foreach($cats as $id=>$name) {
			$catData .= $id." - ".$name."::--::";
			if($data['linkCatId'] == $id) $selCat = $id." - ".$name;
		}
		// Delete last comma
		$catData = substr($catData,0,strlen($catData)-6);
		
		$formField[$i]['fieldName'] = "Kategorie";
		$formField[$i]['fieldType'] = Form::$TYPE_SELECT;
		$formField[$i]['fieldDefaultValue'] = $selCat;
		$formField[$i]['fieldCheck'] = false;
		$formField[$i]['fieldSelectOptions'] = $catData;
		$formField[$i]['fieldSelectMultiple'] = false;
		$i++;
		
		
		$predefLinkText = "Kein vorgefertiger Link::--::";
		$predefLinks = Navigation::getPredefinedLinks();
		foreach($predefLinks as $preLink) {
			$predefLinkText .= $preLink['preName']."::--::";
		}
		// Delete last comma
		$predefLinkText = substr($predefLinkText,0,strlen($predefLinkText)-6);
		
		$formField[$i]['fieldName'] = "Vorgefertigter Link";
		$formField[$i]['formName'] = "preDefined";
		$formField[$i]['fieldType'] = Form::$TYPE_SELECT;
		$formField[$i]['fieldDefaultValue'] = "";
		$formField[$i]['fieldCheck'] = false;
		$formField[$i]['fieldSelectOptions'] = $predefLinkText;
		$formField[$i]['fieldSelectMultiple'] = false;
		$i++;
		
		$formField[$i]['fieldName'] = "URL";
		$formField[$i]['fieldType'] = Form::$TYPE_TEXT;
		$formField[$i]['fieldDefaultValue'] = $data['linkURL'];
		$formField[$i]['fieldCheck'] = false;
		$i++;
		
		$formField[$i]['fieldName'] = "Template";
		$formField[$i]['formName'] = "template";
		$formField[$i]['fieldType'] = Form::$TYPE_SELECT;
		$formField[$i]['fieldDefaultValue'] = $data['linkTemplate'];
		$formField[$i]['fieldCheck'] = false;
		
		$templateKeys = "";
		$templateValues = "";
		foreach(Navigation::$naviTemplates as $key=>$template) {
			$templateValues .= $key."::--::";
			$templateKeys .= $template."::--::";
		}
		$templateKeys = substr($templateKeys,0,strlen($templateKeys)-6);
		$templateValues = substr($templateValues,0,strlen($templateValues)-6);
		$formField[$i]['fieldSelectOptions'] = "Kein Template::--::".$templateValues;
		$formField[$i]['fieldSelectKey'] = "::--::".$templateKeys;
		
		$formField[$i]['fieldSelectMultiple'] = false;
		$i++;
	
	
		$formField[$i]['fieldName'] = "HTML-Code";
		$formField[$i]['formName'] = "htmlCode";
		$formField[$i]['fieldDescription'] = "Wenn du hier HTML-Code angibst, wird der statt dem normalen Link ausgegeben.";
		$formField[$i]['fieldSelectOptions'] = "30_5";
		$formField[$i]['fieldType'] = Form::$TYPE_TEXTAREA;
		$formField[$i]['fieldDefaultValue'] = $data['linkHTML'];
		$formField[$i]['fieldCheck'] = false;
		$i++;
		
		$formField[$i]['fieldName'] = "Aktiv?";
		$formField[$i]['formName'] = "aktiv";
		$formField[$i]['fieldType'] = Form::$TYPE_SELECT;
		$formField[$i]['fieldDefaultValue'] = ($data['linkShow'] == 0) ? 'Ja':"Nein";
		$formField[$i]['fieldCheck'] = false;
		$formField[$i]['fieldSelectOptions'] = "Ja::--::Nein";
		$formField[$i]['fieldSelectMultiple'] = false;
		$i++;
		
		
		$formField[$i]['fieldName'] = "Tab?";
		$formField[$i]['formName'] = "isTab";
		$formField[$i]['fieldDescription'] = "Wenn der Link ein Tab ist, kann eingestellt werden, dass eine Kategorie nur angezeigt wird, wenn der Link angeklickt wurde.";
		$formField[$i]['fieldType'] = Form::$TYPE_SELECT;
		$formField[$i]['fieldDefaultValue'] = ($data['linkTab'] == 0) ? 'Ja':"Nein";
		$formField[$i]['fieldCheck'] = false;
		$formField[$i]['fieldSelectOptions'] = "Ja::--::Nein";
		$formField[$i]['fieldSelectMultiple'] = false;
		$i++;
		
		
		$formField[$i]['fieldName'] = "Nur f&uuml;r Admins?";
		$formField[$i]['formName'] = "onlyAdmins";
		$formField[$i]['fieldType'] = Form::$TYPE_SELECT;
		$formField[$i]['fieldDefaultValue'] = ($data['linkOnlyAdmin'] == 0) ? 'Ja':"Nein";
		$formField[$i]['fieldCheck'] = false;
		$formField[$i]['fieldSelectOptions'] = "Ja::--::Nein";
		$formField[$i]['fieldSelectMultiple'] = false;
		
		$form = new Form();
		$form->initByData("Link bearbeiten",true,"Speichern!",$formField);
		
		// Done?
		if($form->completeCheck()) {
			
			// Create Navigation
			$active = ($_POST['aktiv'][0] == "Ja") ? 0:1;
			// Is Tab?
			$isTab = ($_POST['isTab'][0] == "Ja") ? 0:1;
			// Template?
			$template = ($_POST['template'][0] == "Kein Template") ? "":$_POST['template'][0];
			// Only Admins can see this link?
			$onlyAdmins = ($_POST['onlyAdmins'][0] == "Ja") ? 0:1;
			// Category-Id
			$catId = substr($_POST['Kategorie'][0],0,strpos($_POST['Kategorie'][0]," - "));
			
			$url = $_POST['URL'];
			if($_POST['preDefined'][0] != "Kein vorgefertiger Link") {
				// PredefinedLink selected
				foreach($predefLinks as $preL) {
					if($preL['preName'] == $_POST['preDefined'][0]) {
						if(Settings::getInstance()->getSetting("general.useModRewrite") == 0) {
							$url = $preL['preLinkModRewrite'];
						}else{
							$url = $preL['preLinkNoModRewrite'];
						}
					}
				}
			}
			
			Navigation::editLink($_GET['linkId'],$catId,$_POST['Name'],$url,$template,$_POST['htmlCode'],$active,$isTab,$onlyAdmins);
			General::ouputMessage('Erfolgreich','Der Link wurde erfolgreich gespeichert.','form');
		}else{
			$form->outputFormular('form','formular.html');
		}
	}

	

	/**
	 * DELETE
	 */
	if(isset($_GET['do']) && $_GET['do'] == "del" && isset($_GET['linkId']) && is_numeric($_GET['linkId'])) {
		require_once './kernel/formCheck.php';
		$form = new FormCheck("Bist du sicher?","L&ouml;schen");
		// Done?
		if($form->completeCheck()) {
			if($form->isChecked()) {
				// Delete navigation
				Navigation::deleteLink($_GET['linkId']);
			}
		}else{
			$form->outputFormular('form','formular.html');
		}
	}

	
	if(isset($_GET['do']) && $_GET['do'] == "up" && isset($_GET['linkId']) && is_numeric($_GET['linkId'])) {
		$unsortedCats = Navigation::getLinksByCatId($_GET['catId']);
		// Create Array
		$unsortedArray = array();
		foreach($unsortedCats as $data) {
			$unsortedArray[] = $data['id'];
		}
		
		$sortedArray = General::orderArray($unsortedArray,"up",$_GET['linkId']);
		if($sortedArray == false) {
			General::ouputMessage('Fehler','Der Link ist schon ganz oben.','form');
		}else{
			// Save
			foreach($sortedArray as $orderId=>$id) {
				DB::getInstance()->query("
					UPDATE
						".Tables::$naviLinksTbl."
					SET
						orderId = '".$orderId."'
					WHERE
						id = '".$id."'");
			}
			General::ouputMessage('Erfolgreich','Der Link wurde erfolgreich verschoben.','form');
		}
	}
	
	if(isset($_GET['do']) && $_GET['do'] == "down" && isset($_GET['linkId']) && is_numeric($_GET['linkId'])) {
		$unsortedCats = Navigation::getLinksByCatId($_GET['catId']);
		// Create Array
		$unsortedArray = array();
		foreach($unsortedCats as $data) {
			$unsortedArray[] = $data['id'];
		}
		
		$sortedArray = General::orderArray($unsortedArray,"down",$_GET['linkId']);
	
		if($sortedArray == false) {
			General::ouputMessage('Fehler','Der Link ist schon ganz unten.','form');
		}else{
			// Save
			foreach($sortedArray as $orderId=>$id) {
				DB::getInstance()->query("
					UPDATE
						".Tables::$naviLinksTbl."
					SET
						orderId = '".$orderId."'
					WHERE
						id = '".$id."'");
			}
			General::ouputMessage('Erfolgreich','Der Link wurde erfolgreich verschoben.','form');
		}
	}
	
	$links = Navigation::getLinksByCatId($_GET['catId']);
	General::getTpl()->assign('links',$links);
	
	General::getTpl()->assign('body',
		General::getTpl()->fetch('../admin/navigationLinks.html'));
}
?>